<template>
  <!-- 展开/折叠 -->
  <div class="hu-collapse">
    <div ref="huCollapse" :style="showStyle" class="hu-collapse--cont">
      <slot name="default" />
    </div>
    <div class="hu-collapse--link" v-if="showBtn">
      <span @click="toggleContent">
        {{ isCollapsed ? '展开' : '收起' }}
        <van-icon name="play" :class="isCollapsed?'':'hu-collapse--rotate90'" />
      </span>
    </div>
  </div>
</template>

<script>
export default {
  name: "HuCollapse",
  props: {
    // 需要进行折叠展开的高度，超高该高度出现展开按钮
    height: {
      type: Number,
      default: 0,
    },
    // 收起时主体部分额外样式类名
    closeClass: {
      type: String,
      default: "",
    },
  },
  data() {
    return {
      isCollapsed: true, // 是否为折叠状态
      showBtn: false,
      C_height: "", // 元素完整高度
    };
  },
  computed: {
    // 显示样式
    showStyle() {
      if (!this.showBtn) return {};
      if (this.isCollapsed) return { height: this.height + "px" };
    },
  },
  mounted() {
    this.C_height = this.$refs.huCollapse.offsetHeight; // 获取当前元素高度
    this.showBtn = this.C_height > this.height; // 是否展示按钮，当完整高度大于指定高度时展示
  },

  methods: {
    toggleContent() {
      this.isCollapsed = !this.isCollapsed;
    },
  },
};
</script>

<style lang="less" scoped>
.hu-collapse {
  .hu-collapse--cont {
    overflow: hidden; //溢出隐藏
  }
  .hu-collapse--link {
    margin-top: 10px;
    text-align: right;
    font-size: 12px;
    color: @blue;
  }
  .hu-collapse--rotate90 {
    transform: rotate(-90deg);
  }
}
</style>